package com.qys.training.server.config;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.ClientNetworkConfig;
import com.hazelcast.core.HazelcastInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
@Configuration
public class HazelcastClientConfig {
    @Value("${qys.training.hazelcast.client.group-name}")
    String groupName;


    private static final Logger logger =
            LoggerFactory.getLogger(HazelcastClientConfig.class);

    @Bean
    public HazelcastInstance hazelcastClient() {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.setProperty("hazelcast.logging.type", "slf4j");

        clientConfig.getGroupConfig().setName(groupName);
        ClientNetworkConfig networkConfig =
                clientConfig.getNetworkConfig();
        networkConfig.setAddresses(Arrays.asList(getAddress()))
                .setSmartRouting(true)
                .setRedoOperation(true)
                .setConnectionTimeout(5000)
                .setConnectionAttemptLimit(60);
        HazelcastInstance client =
                HazelcastClient.newHazelcastClient(clientConfig);
        logger.info("Hazelcast 客户端启动成功");
        return client;
    }

    private String getAddress() {
        String address = "127.0.0.1";
        return address;
    }
}
